/**
 * Created by oneminute on 7/17/14.
 */

var lz = require("../lib/utils/modules")();

exports.get = function (_req, _res, _opts, _callback) {
    var suid = lz.Utils.decrypt(_opts.suid);
    var page = _opts.p * lz.Config.webServer.page;

    lz.Logger.debug("/shopgoods: suid: %s, uid: %s, p: %d", suid, _req.query.uid, page);
    if (suid == _req.query.uid) {
        var user = _opts.user;
        //user.load2({method: "byId"}, function (err, _user) {
        if (user) {
            lz.Utils.getSubList(page, user.commodities, function (_items, _isEnd) {
                var commodities = [];
                _items.forEach(function (_commodity) {
                    commodities.push(_commodity.getInfo('t'));
                });
                _callback(null, null, commodities, _isEnd);
            });
        } else {
            _callback(lz.Config.errors.getError(lz.Config.errors.REQUEST_TEMPUSER_ERROR));
        }
        //});
    } else {
        var user = new lz.User({id: suid});
        user.load({method: "byId"}, function (err, _user) {
            lz.Logger.debug("/shopgoods: 加载用户.", _user.getInfo());
            if (_user && _user.inbusiness) {
                lz.Utils.getSubList(page, _user.commodities, function (_items, _isEnd) {
                    var commodities = [];
                    _items.forEach(function (_commodity) {
                        commodities.push(_commodity.getInfo('t'));
                    });
                    _callback(null, null, commodities, _isEnd);
                });
            } else {
                // 用户店铺已停止运营
                _callback(lz.Config.errors.getError(lz.Config.errors.REQUEST_SHOP_NOT_IN_BUSINESS));
            }
        });
    }
};